import router from './router'
import store from '@/store'
import { getStorage } from '@/utils/storage'
import { Message } from 'element-ui'

const whiteList = ['/login', '/404']
const time = 'time-stamp-key'
const timeLimit = 2 * 60 * 60 * 1000 // toekn 前端时间戳验证

// 前置路由守卫
router.beforeEach((to, from, next) => {
  if (whiteList.some(item => item === to.path)) {
    next()
    return
  }
  if (store.state.userInfo.token) {
    if (Date.now() - getStorage(time) > timeLimit) {
      Message.warning('登录认证过期,请重新登录')
      store.commit('logOut')
      next('/login')
    } else {
      next()
    }
  } else {
    next('/login')
  }
})
